package com.huilian.iotbox.admin.wap.controller;

import com.huilian.iotbox.admin.wap.service.WeiXinService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.WeiXinDto;
import com.huilian.iotbox.data.enums.ApiParamEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author chenqiwen
 * @CreateDate: 2022-05-13-13-49
 * @Description:
 */
@Slf4j
@Controller
@RequestMapping("/api")
public class WeiXinController extends UserInfoController {

    @Autowired
    private WeiXinService weiXinService;


    /**
     * 获取到用户信息回调
     *
     * @param request
     * @return
     */
    @GetMapping("/biz/login")
    public String login(HttpServletRequest request) throws Exception {
        return weiXinService.login(request);
    }


    @GetMapping("/biz/outPutUserInfo")
    public String outPutUserInfo(HttpServletRequest request) throws UnsupportedEncodingException {
        return weiXinService.outPutUserInfo(request);
    }


    /**
     * 微信小程序登录
     *
     * @param request
     * @param weiXinDto
     * @return
     * @throws UnsupportedEncodingException
     */
    @ResponseBody
    @PostMapping("/applet/login")
    public ServerResponse appletLogin(HttpServletRequest request, @RequestBody WeiXinDto weiXinDto) throws IOException {
        return weiXinService.appletLogin(weiXinDto, request);
    }

    /**
     * 获取jssdk配置参数
     *
     * @param weiXinDto
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/weixin/getWxJsSdkConfig")
    public ServerResponse getWxJsSdkConfig(WeiXinDto weiXinDto, HttpServletRequest request) throws Exception {
        return weiXinService.getWxJsSdkConfig(weiXinDto);
    }

    /**
     * 绑定微信
     */
    @ResponseBody
    @PostMapping("/applet/bindWeXin")
    public ServerResponse bindWeXin(HttpServletRequest request, @RequestBody WeiXinDto weiXinDto) throws IOException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        weiXinDto.setUserInfo(userInfo);
        return weiXinService.bindWeXin(weiXinDto);
    }


    /**
     * 获取手机号
     */
    @ResponseBody
    @PostMapping("/weChatApplet/getPhoneNumber")
    public ServerResponse getPhoneNumber(HttpServletRequest request, @RequestBody WeiXinDto weiXinDto) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        weiXinDto.setUserInfo(userInfo);
        return weiXinService.getPhoneNumber(weiXinDto);
    }
}
